//
//  FoodMainViewController.m
//  ArtOfLiving
//
//  Created by 刘虎 on 16/4/29.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "FoodMainViewController.h"
#import "FoodMainTableViewCell.h"
#import "FoodUpdateTableViewCell.h"
#import "FoodMainModel.h"
#import "FoodScrollView.h"
#import "FoodDetailViewController.h"
#import "FoodThemeMoreListViewController.h"
#import "FoodUpdateMoreListViewController.h"
#import "FoodThemeListViewController.h"

typedef enum {
    
    MoreBtnTagTheme = 100,
    MoreBtnTagUpdata
}MoreBtnTag;

@interface FoodMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *themeDataSourceArr;               //存储主题存放在model中的数据
@property (nonatomic, strong) NSMutableArray *recommendDataSourceArr;           //存储推荐model中的数据
@property (nonatomic, strong) NSMutableArray *updateDataSourceArr;              //存放最近更新model中的数据
@property (nonatomic, strong) NSMutableDictionary *parameter;                   //请求参数

@end

@implementation FoodMainViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self fetchDataSourceWithUrlString:kFoodMainList parameter:self.parameter];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark - -------TableView的代理方法-------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.updateDataSourceArr.count>0?2:0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return self.themeDataSourceArr.count;
    } else {

        return self.updateDataSourceArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCell *cell = nil;
    id model = nil;
    if (indexPath.section == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"foodMainCELL" forIndexPath:indexPath];
        model = self.themeDataSourceArr[indexPath.row];
    } else {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"foodUpdateCELL" forIndexPath:indexPath];
        model = self.updateDataSourceArr[indexPath.row];
    }
    [cell setCellWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    if (indexPath.section == 0) {
        
        FoodThemeListViewController *foodThemeListVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"FoodThemeListViewController"];
        FoodMainModel *model = self.themeDataSourceArr[indexPath.row];
        foodThemeListVC.ID = model.ID;
        [self.navigationController pushViewController:foodThemeListVC animated:YES];
    }   else {
        
        FoodDetailViewController *foodDetailVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"FoodDetailViewController"];
        FoodMainModel *model = self.updateDataSourceArr[indexPath.row];
        foodDetailVC.Id = model.ID;
        [self.navigationController pushViewController:foodDetailVC animated:YES];
    }
}

//设置分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    UILabel *greenMarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 10, 30)];
    greenMarkLabel.backgroundColor = [UIColor greenColor];
    [sectionHeadView addSubview:greenMarkLabel];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(kScreenWidth-70, 10, 60, 30);
    [btn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"更多》" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sectionHeadView addSubview:btn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 100, 30)];
    if (section == 0) {
        
        titleLabel.text = @"主题";
        btn.tag = MoreBtnTagTheme;
    } else {
        
        titleLabel.text = @"最近更新";
        btn.tag = MoreBtnTagUpdata;
    }
    [sectionHeadView addSubview:titleLabel];
    return sectionHeadView;
    
}


#pragma mark - ------设置头视图------
- (void)setTableViewHeaderView {
    
    FoodScrollView *foodScrollView = [[FoodScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200) pictureArr:self.recommendDataSourceArr];
    self.tableView.tableHeaderView = foodScrollView;
}

#pragma mark - --------分区视图btn方法回调---------
- (void)moreBtnClick:(UIButton *)sender {
    
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewController = nil;
    if (sender.tag == MoreBtnTagTheme) {
        
        viewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"FoodThemeMoreListViewController"];
    } else {
        
        viewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"FoodUpdateMoreListViewController"];
    }
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - --------数据请求与解析-------
- (void)fetchDataSourceWithUrlString:(NSString *)urlString parameter:(NSMutableDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlString paramenters:parameter success:^(id data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       
        //主题
        NSArray *themeArr = dic[@"data"][@"theme"];
        for (NSDictionary *item in themeArr) {
            
            FoodMainModel *foodMainModel = [[FoodMainModel alloc]init];
            [foodMainModel setValuesForKeysWithDictionary:item];
            [self.themeDataSourceArr addObject:foodMainModel];
        }
        //推荐
        NSArray *recommendArr = dic[@"data"][@"recommend"];
        for (NSDictionary *item in recommendArr) {
            
            FoodMainModel *foodMainModel = [[FoodMainModel alloc]init];
            [foodMainModel setValuesForKeysWithDictionary:item];
            [self.recommendDataSourceArr addObject:foodMainModel.img];
        }
             NSLog(@"%ldcount",self.recommendDataSourceArr.count);
        //最近更新
        NSArray *updateArr = dic[@"data"][@"update"];
        for (NSDictionary *item in updateArr) {
            
            FoodMainModel *foodMainModel = [[FoodMainModel alloc]init];
            [foodMainModel setValuesForKeysWithDictionary:item];
            [self.updateDataSourceArr addObject:foodMainModel];
        }
        
        [self setTableViewHeaderView];
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"食物数据请求出错");
    } view:self.view];
}

#pragma mark - -------属性懒加载--------
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-44) style:UITableViewStyleGrouped];
        [_tableView registerNib:[UINib nibWithNibName:@"FoodMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"foodMainCELL"];
        [_tableView registerNib:[UINib nibWithNibName:@"FoodUpdateTableViewCell" bundle:nil] forCellReuseIdentifier:@"foodUpdateCELL"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)themeDataSourceArr {
    
    if (!_themeDataSourceArr) {
        
        _themeDataSourceArr = [[NSMutableArray alloc] init];
    }
    return _themeDataSourceArr;
}

- (NSMutableDictionary *)parameter {
    
    if (!_parameter) {
        
        _parameter = [[NSMutableDictionary alloc] init];
    }
    return _parameter;
}

- (NSMutableArray *)updateDataSourceArr {
    
    if (!_updateDataSourceArr) {
        
        _updateDataSourceArr = [[NSMutableArray alloc] init];
    }
    return _updateDataSourceArr;
}

- (NSMutableArray *)recommendDataSourceArr {
    
    if (!_recommendDataSourceArr) {
        
        _recommendDataSourceArr = [[NSMutableArray alloc] init];
    }
    return _recommendDataSourceArr;
}


@end
